package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper roleMapper;

    @Override
    public Set<String> findRoleByIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> qw = new LambdaQueryWrapper<>();
        qw.in(DtsRole::getId, roleIds);
        List<DtsRole> roles = roleMapper.selectList(qw);

        //stream 转换为流， map： 要映射成一个什么样的对象  collect：指定返回的集合类型
        Set<String> roleStrs = roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return roleStrs;
    }

    @Override
    public List<DtsRole> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole> ipage = new Page<>(page, limit);

        QueryWrapper<DtsRole> qw = new QueryWrapper();
        if(! StringUtils.isEmpty(rolename)) {
            qw.like("name", rolename);
        }

        if( !StringUtils.isEmpty(sort) &&!StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        roleMapper.selectPage(ipage, qw);
        return ipage;
    }

    @Override
    public void update(DtsRole role) {
        roleMapper.updateById(role);
    }

    @Autowired
    DtsAdminMapper adminMapper;

    @Autowired
    DtsPermissionMapper permissionMapper;

    @Override
    public boolean delete(DtsRole role) {
        //如果该角色被管理员使用，则不能删除
        LambdaQueryWrapper<DtsAdmin> qw1 = new LambdaQueryWrapper<>();
        //指定 只查询  roleIds 列，其他列不查询
        qw1.select(DtsAdmin::getRoleIds);
        List<DtsAdmin> adminList = adminMapper.selectList(qw1);
        for (DtsAdmin admin : adminList) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                //如果数据查询到的角色id与 要删除的角色id相同，则不能删除
                if(roleId.equals(role.getId())) {
                    return false;
                }
            }
        }

        //如果该角色关联了权限，则不能删除
        LambdaQueryWrapper<DtsPermission> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsPermission::getRoleId, role.getId());
        Integer count = permissionMapper.selectCount(qw2);
        if(count >= 1) {
            return  false;
        }

        //如果没有被管理员和权限关联，则删除
        roleMapper.deleteById(role.getId());
        return true;
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole dtsRole = roleMapper.selectById(roleId);
        if("超级管理员".equalsIgnoreCase(dtsRole.getName())) {
            return true;
        }
        return false;
    }
}
